package com.loongtech.bi.action.authManagement;

import com.loongtech.bi.entity.system.EntitySysFunction;
import com.loongtech.bi.manager.system.SysFunctionManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysModelManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * Author: yuri
 * Description: 功能模块
 * Date: 15:42 2017/4/28
 */
@RestController
@RequestMapping("/function")
@Scope("prototype")
public class SysFunctionAction implements Serializable {
    private static final long serialVersionUID = 1L;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SysModelManager sysModelManager;
    @Resource
    private SysFunctionManager sysFunctionManager;
    @Resource
    private SysRoleFunctionManager sysRoleFunctionManager;

    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> list() {
        List<EntitySysFunction> list;
        try {
            list = sysFunctionManager.findByQuery("from EntitySysFunction order by id asc");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> add(EntitySysFunction function) {
        String name = function.getName();
        // 判断用户name是否已经被注册
        try {
            if (null != sysFunctionManager.getBy(EntitySysFunction.K.name, name)) {
                return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
            }
            if (null != sysFunctionManager.getBy(EntitySysFunction.K.id, function.getId())) {
                return new ResultData<>(RetCode.eIdExist.getErrorcode(), RetCode.eIdExist.getErrorMsg());
            }
            sysFunctionManager.persist(function);
            sysLogManager.addLog(session.getUserName(), "增加功能页面成功:" + name);
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(EntitySysFunction function) {
        try {
            if (null == sysModelManager.get(function.getModelId())) {
                sysLogManager.addLog(session.getUserName(), "模块修改失败,不存在模块modelId=" + function.getModelId());
                return new ResultData<>(RetCode.eModuleEmpty.getErrorcode(), RetCode.eModuleEmpty.getErrorMsg());
            }
            EntitySysFunction cur = sysFunctionManager.get(function.getId());
            if (cur == null) {
                sysLogManager.addLog(session.getUserName(), "功能修改失败,不存在功能functionId=" + function.getId());
                return new ResultData<>(RetCode.eFunctionEmpty.getErrorcode(), RetCode.eFunctionEmpty.getErrorMsg());
            }
            // 保存
            sysFunctionManager.merge(function);
            sysLogManager.addLog(session.getUserName(), "功能修改成功!functionId=" + function.getId());
            return ResultData.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "delete.do", method = {RequestMethod.POST, RequestMethod.GET})
    public ResultData<String> delete(@RequestParam(value = "id") Integer id) {
        try {
            if (null != sysFunctionManager.get(id)) {
                sysFunctionManager.removeById(id);
                //删除角色功能表相应数据
                sysRoleFunctionManager.excuteUpdateByHql("delete from EntitySysRoleFunction where functionId = ?", id);
                sysLogManager.addLog(session.getUserName(), "功能删除成功!funcId=" + id);
                return ResultData.success();
            }
            return new ResultData<>(RetCode.eFunctionEmpty.getErrorcode(), RetCode.eFunctionEmpty.getErrorMsg());
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}
